$(function(){
	loadLeftMenuList();
});

//页面左侧 - 目录结构
function loadLeftMenuList(){
	$.ajax({
		url : $.ctx + "/menu/getMenuList.do",
		type : "post",
		dataType : "json",
		success : function(menuList){
			var htmlInfo = "<ul><li><a href='javascript:void(0);'>发布系统</a><ul>";
			$.each(menuList, function(n, authority) {//1级目录
				htmlInfo += "<li style='display: list-item;'>";
				htmlInfo += "<a href='javascript:showMenuInfo(1,"+authority.id+");'>"+authority.authorityname+"</a>";
				htmlInfo += "<ul>";
				
				$.each(authority.catalogList, function(n, catalog) {//2级目录
					htmlInfo += "<li style='display: list-item;'>";
					htmlInfo += "<a href='javascript:showMenuInfo(2,"+catalog.id+");'>"+catalog.catalogname+"</a>";
					htmlInfo += "<ul>";
					
					$.each(catalog.resourceList, function(n, resource) {//3级目录
						htmlInfo += "<li style='display: list-item;'>";
						htmlInfo += "<a href='javascript:showMenuInfo(3,"+resource.id+");'>"+resource.resourcename+"</a>";
						htmlInfo += "</li>";
					});
					
					htmlInfo += "</ul>";
					htmlInfo += "</li>";
				});
				
				htmlInfo += "</ul>";
				htmlInfo += "</li>";
			});
			htmlInfo += "</ul></li></ul>";
			$("#family-tree-vertical").html(htmlInfo);
		}
     });
}

//加载 - 4级目录（功能）List
function loadMenuLv4Info(resourceId){
	menuLv4InfoTable = $('#menulv4info').dataTable( {
		"paging":   false,
	    "ordering": false,
	    "info":     false,
	    "searching":false,
	    "destroy": true,
        "ajax": $.ctx + "/menu/getLv4MenuList.do?resourceid="+resourceId,
        "columns": [
            { "data": "actionname" },
            { "data": "isbutton" },
            { "data": "sort" },
            { "data": null }
        ],
        "columnDefs": [
               {"targets": 3,
                "render" : function(data, type, row, where) {
                	var html = '';
                	html += '<button type="button" class="btn btn-default btn-xs" onclick="javascript:loadMenuLv4InfoSingleton('+data.id+');">修改</button>&nbsp;&nbsp;';
                	html += '<button type="button" class="btn btn-default btn-xs" onclick="javascript:delAction('+data.id+');">删除</button>';
                    return html;}},
               {"targets": 1,
	           	"render" : function(data, type, row, where) {
	           			return data == 0?"是":"否";
	           }}],
    });
}

//加载 - 4级目录（功能）单个
function loadMenuLv4InfoSingleton(actionId){
	$.ajax({
		url : $.ctx + "/menu/getMenuInfo.do",
		type : "post",
		data : {
			"menuLv" : 4,
			"menuId" : actionId
		},
		dataType : "json",
		async : "false",
		success : function(data){
			$("#actionid").val(data.action.id);
			$("#actionurl").val(data.action.url);
			$("#actionname").val(data.action.actionname);
			$("#actionsort").val(data.action.sort);
			$("#isbutton").val(data.action.isbutton);
			$('#isbutton').selectpicker('refresh');//根据select/option, 刷新select/div的值
			$("#actioncode").val(data.action.code);
			$("#actionnote").val(data.action.note);
			$('#menulv4infoa').trigger('click');
			$('#createActionButton').hide();//保存按钮 隐藏
			$('#updateActionButton').show();//修改按钮 显示
		}
	});
}
//下拉框 - 菜单等级
function selectMenuLv(selectDom){
	var optionVal = $(selectDom).val();//选中值
	
	if(optionVal == 1){//选择 -> 菜单等级1, 隐藏一级菜单、隐藏二级菜单
		$("#menulv1div").hide();
		$("#menulv2div").hide();
		$("#menulv3urldiv").hide();
	}else if(optionVal == 2){//选择 -> 菜单等级2, 显示一级菜单、隐藏二级菜单
		getLv1MenuData();
		$("#menulv1div").slideDown("slow");
		$("#menulv2div").hide();
		$("#menulv3urldiv").hide();
	}else if(optionVal == 3){//选择 -> 菜单等级3, 显示一级菜单、隐藏二级菜单
		getLv1MenuData();
		$("#menulv1div").slideDown("slow");
		$("#menulv2div").slideDown("slow");
		$("#menulv3urldiv").slideDown("slow");
	}
}

//加载下拉框 - 从属一级菜单
function getLv1MenuData(){
	$.ajax({
		url : $.ctx + "/menu/getLv1MenuList.do",
		type : "post",
		dataType : "json",
		success : function(sysAuthorityList){
			var sel = document.getElementById("menulv1"); 
			sel.innerHTML = "";//清空, 防止多次加载, 内容重复
			$.each(sysAuthorityList, function(n, authority) {
				sel.options.add(new Option(authority.authorityname, authority.id));
			});
			$('#menulv1').selectpicker('refresh');//根据select/option, 刷新select/div的值
			//$('#menulv1').prop('disabled', true);//禁用
		}
	});
}

//加载下拉框 - 从属二级菜单
function getLv2MenuData(selectDom){
	var optionVal = $(selectDom).val();//选中值
	$.ajax({
		url : $.ctx + "/menu/getLv2MenuList.do",
		type : "post",
		dataType : "json",
		data : {
			"authorityId" : optionVal
		},
		success : function(sysCatalogList){
			var sel= document.getElementById("menulv2"); 
			sel.innerHTML = "";//清空, 防止多次加载, 内容重复
			$.each(sysCatalogList, function(n, catalog) {
				sel.options.add(new Option(catalog.catalogname, catalog.id));
			});
			$('#menulv2').selectpicker('refresh');//根据select/option, 刷新select/div的值
			//$('#menulv1').prop('disabled', true);//禁用
		}
	});
}

//下拉框 - 是否是按钮
function isbuttonchange(selectDom){
	var v = $(selectDom).val();//选中值 //0是按钮 1不是按钮
	if(v==0){
		$("#buttonCodeDiv").slideDown("slow");
	}else if(v==1){
		$("#buttonCodeDiv").slideUp("slow");
	}
}

//保存 - 新建 - 目录菜单
function createMenu(){
	changeButtonStatusForLoading('createMenuButton');//禁用按钮
	var validResult = $("#menuform").valid();//检查form
	if(validResult == false){
		changeButtonStatusForNormal('createMenuButton', '保存');//放开按钮
		return;
	}
	$.ajax({
		url : $.ctx + "/menu/saveMenu.do",
		type : "post",
		dataType : "json",
		data : {
			"level" : $("#level").val(),
			"menulv1" : $("#menulv1").val(),
			"menulv2" : $("#menulv2").val(),
			"pageurl" : $("#pageurl").val(),
			"menuname" : $("#menuname").val(),
			"sort" : $("#sort").val()
		},
		success : function(result){
			if (result.msg == '操作成功') {
				location.reload();
			}else{
				toastr.warning(result.msg);
			}
		}
    });
	changeButtonStatusForNormal('createMenuButton', '保存');//放开按钮
}

//保存 - 新建 - 4级菜单(功能)
function createAction(){
	changeButtonStatusForLoading('createActionButton');//禁用按钮
	var validResult = $("#menulv4from").valid();//检查form
	if(validResult == false){
		changeButtonStatusForNormal('createActionButton', '保存');//放开按钮
		return;
	}
	$.ajax({
		url : $.ctx + "/menu/saveMenu.do",
		type : "post",
		dataType : "json",
		data : {
			"level" : 4,
			"menulv3" : $("#pageInfo #editMenuId").val(),
			"pageurl" : $("#actionurl").val(),
			"menuname" : $("#actionname").val(),
			"sort" : $("#actionsort").val(),
			"isbutton" : $("#isbutton").val(),
			"code" : $("#actioncode").val(),
			"note" : $("#actionnote").val()
		},
		success : function(result){
			if (result.msg == '操作成功') {
				$('#hidenpagefunctiondiv').trigger('click');
				toastr.success(result.msg);
				loadMenuLv4Info($("#pageInfo #editMenuId").val()); // 刷新表格数据
			}else{
				toastr.warning(result.msg);
			}
		}
    });
	changeButtonStatusForNormal('createActionButton', '保存');//放开按钮
}

//修改菜单(1,2,3级目录菜单)
function updateMenu(){
	$.ajax({
		url : $.ctx + "/menu/updateMenu.do",
		type : "post",
		dataType : "json",
		async : "false",
		data : {
			"editMenuLv"	: $("#pageInfo #editMenuLv").val(),
			"editMenuId"	: $("#pageInfo #editMenuId").val(),
			"editMenuUrl"	: $("#pageInfo #editMenuUrl").val(),
			"editMenuName"	: $("#pageInfo #editMenuName").val(),
			"editMenuSort"	: $("#pageInfo #editMenuSort").val()
		},
		success : function(result){
			if (result.msg == '操作成功') {
				toastr.success(result.msg);
				loadLeftMenuList();
			}else{
				toastr.warning(result.msg);
			}
		}
    });
}

//修改菜单(4级目录菜单)
function updateAction(){
	$.ajax({
		url : $.ctx + "/menu/updateMenu.do",
		type : "post",
		dataType : "json",
		async : "false",
		data : {
			"editMenuLv"	: 4 ,
			"editMenuId"	: $("#menulv4from #actionid").val(),
			"editMenuUrl"	: $("#menulv4from #actionurl").val(),
			"editMenuName"	: $("#menulv4from #actionname").val(),
			"editMenuSort"	: $("#menulv4from #actionsort").val(),
			"isbutton"		: $("#menulv4from #isbutton").val(),
			"actioncode"	: $("#menulv4from #actioncode").val(),
			"actionnote"	: $("#menulv4from #actionnote").val()
		},
		success : function(result){
			if (result.msg == '操作成功') {
				$('#hidenpagefunctiondiv').trigger('click');
				toastr.success(result.msg);
				loadLeftMenuList();
				loadMenuLv4Info($("#pageInfo #editMenuId").val()); // 刷新表格数据
			}else{
				toastr.warning(result.msg);
			}
		}
    });
}

//删除菜单(1,2,3级目录菜单)
function delMenu(){
	$.ajax({
		url : $.ctx + "/menu/delMenu.do",
		type : "post",
		dataType : "json",
		async : "false",
		data : {
			"editMenuLv" : $("#pageInfo #editMenuLv").val(),
			"editMenuId" : $("#pageInfo #editMenuId").val()
		},
		success : function(result){
			if (result.msg == '操作成功') {
				toastr.success(result.msg);
				loadLeftMenuList();
			}else{
				toastr.warning(result.msg);
			}
		}
    });
}

//删除菜单(4级目录菜单、功能)
function delAction(menuId){
	if (confirm("确定要删除这个页面功能？如果删除，将不能访问这个功能地址。") == false) {
        return;
    }
	$.ajax({
		url : $.ctx + "/menu/delMenu.do",
		type : "post",
		dataType : "json",
		async : "false",
		data : {
			"editMenuLv" : 4,
			"editMenuId" : menuId
		},
		success : function(result){
			if (result.msg == '操作成功') {
				toastr.success(result.msg);
				loadMenuLv4Info($("#pageInfo #editMenuId").val()); // 刷新表格数据
			}else{
				toastr.warning(result.msg);
			}
		}
    });
}

//加载 - 目录菜单 详细信息
function showMenuInfo(menuLv, menuId){
	//访问后台获取按钮信息
	$.ajax({
		url : $.ctx + "/menu/getMenuInfo.do",
		type : "post",
		dataType : "json",
		async : "false",
		data : {
			"menuLv" : menuLv,
			"menuId" : menuId
		},
		success : function(result){
			if(menuLv == 1){
				showMenuInfoLv1(result);
			}else if(menuLv == 2){
				showMenuInfoLv2(result);
			}else if(menuLv == 3){
				showMenuInfoLv3(result);
			}
		}
    });
}

//加载 - 1级目录 详细信息
function showMenuInfoLv1(menuInfo){
	$("#editMenuUrlDiv").slideUp("slow");
	$("#editMenuLvName").val("一级菜单");
	$("#editMenuLv").val(1);
	$("#editMenuId").val(menuInfo.authority.id);
	$("#editRelyMenuLv1").val('');
	$("#editRelyMenuLv2").val('');
	$("#editMenuUrl").val('');
	$("#editMenuName").val(menuInfo.authority.authorityname);
	$("#editMenuSort").val(menuInfo.authority.sort);
	$("#menulv4infodiv").slideUp("slow");
}
//加载 - 2级目录 详细信息
function showMenuInfoLv2(menuInfo){
	$("#editMenuUrlDiv").slideUp("slow");
	$("#editMenuLvName").val("二级菜单");
	$("#editMenuLv").val(2);
	$("#editMenuId").val(menuInfo.catalog.id);
	$("#editRelyMenuLv1").val(menuInfo.authority.authorityname);
	$("#editRelyMenuLv2").val('');
	$("#editMenuUrl").val();
	$("#editMenuName").val(menuInfo.catalog.catalogname);
	$("#editMenuSort").val(menuInfo.catalog.sort);
	$("#menulv4infodiv").slideUp("slow");
}
//加载 - 3级目录 详细信息
function showMenuInfoLv3(menuInfo){
	$("#editMenuUrlDiv").slideDown("slow");
	$("#editMenuLvName").val("三级菜单");
	$("#editMenuLv").val(3);
	$("#editMenuId").val(menuInfo.resource.id);
	$("#editRelyMenuLv1").val(menuInfo.authority.authorityname);
	$("#editRelyMenuLv2").val(menuInfo.catalog.catalogname);
	$("#editMenuUrl").val(menuInfo.resource.url);
	$("#editMenuName").val(menuInfo.resource.resourcename);
	$("#editMenuSort").val(menuInfo.resource.sort);
	$("#menulv4infodiv").slideDown("slow");
	loadMenuLv4Info(menuInfo.resource.id);
}